<template>
  <el-dialog
    title="设置标签"
    :visible.sync="visible"
    width="40%"
    :before-close="handleClose">
      <div class="set-material-tags-main">
        <el-autocomplete
          v-model="tagName"
          style="width: 100%"
          :fetch-suggestions="querySearch"
          @keyup.enter.native="handleAddLable"
          placeholder="搜索标签，回车键确认"
          suffix-icon="el-icon-search"
        ></el-autocomplete>
        <div class="tags-list">
          <div v-for="(tag, index) in labelList" :key="index" @click="onTiggerTag(tag)">
            <div :class="['tags-item', tag.active === true ? 'tag-acitve': '']">{{ tag.tagName }}</div>
          </div>
        </div>
      </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="onHanldSubmit">保存</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { fetchMeterialTags, updateMeterial } from "@/api/market/material";
export default {
  data() {
    return {
      loading: false,
      labelList: [],
      tagName: ''
    }
  },
  props: ['visible', 'material'],
  mounted() {
    this.fetchMeterialTagList()
    
  },
  watch: {
    material(newMaterial) {
      this.intersectionArry(newMaterial.tagList)
    }
  },
  methods: {
    // 获取素材标签列表
    async fetchMeterialTagList() {
      try {
        let data = await fetchMeterialTags()
        if (data.code === 200) {
          this.labelList = data?.rows || []
          return data?.rows
        }
      } catch (error) {
        this.$message.error("系统异常");
      }
    },
    intersectionArry(tags) {
      this.labelList.find(item => {
        if (tags.some(tag => item.id === tag.id)) {
          item.active = true
        } else {
          item.active = false
        }
      })
    },
    querySearch(queryString, cb) {
      let results = []
      if (queryString) {
        const findout = this.labelList.filter(this.createFilter(queryString))
        results = findout.map(x => ({...x, value: x.tagName}))
      } else {
        results = this.labelList.map(x => ({...x, value: x.tagName}))
      }
      cb(results);
    },

    createFilter(queryString) {
      return (restaurant) => {
        return (restaurant.tagName.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },
    handleClose() {
      this.$emit('onCloseDialog');
    },
    onTiggerTag(tag) {
      let arr = this.labelList.map(item => {
        if (item.id === tag.id) {
          item.active = !item.active
        }
        return item
      })
      this.labelList = arr
    },
    async onHanldSubmit() {
      let ids = []
      this.labelList.map(item => {
        if (item.active) {
          ids.push(item.id)
        }
      })
      await updateMeterial({id: this.material.id, tagIds: ids.toString()})
      this.$emit('onCloseDialog');
    }
  },
}
</script>
<style lang="scss" scoped>
.set-material-tags-main {
  .tags-list {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    margin-top: 10px;
    .tags-item {
      list-style: none;
      font-feature-settings: "tnum", "tnum";
      display: inline-block;
      height: auto;
      margin: 0 8px 0 0;
      padding: 0 7px;
      font-size: 12px;
      line-height: 20px;
      white-space: nowrap;
      background: #fafafa;
      border: 1px solid #e9e9e9;
      border-radius: 4px;
      opacity: 1;
      transition: all .3s;
      margin-bottom: 10px;
      cursor: pointer;
      &.tag-acitve {
        color: #3070ff;
        background: #f0f7ff;
        border-color: #abceff;
      }
    }
  }
}
</style>